package com.weixin.common.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.weixin.common.common.PageRequest;
import com.weixin.common.common.R;
import com.weixin.common.common.ResponseResult;
import com.weixin.common.model.dto.SaveUserRoleDto;
import com.weixin.common.model.dto.UserAddDto;
import com.weixin.common.model.so.SimpleSo;
import com.weixin.common.service.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

/**
 * @author ZhouShenBin
 * @date 2023/12/18 10:26
 * @description
 */

@RestController
@RequestMapping("/user")
@Tag(name = "用户管理")
public class SysUserController {

    @Resource
    private SysUserService sysUserService;

    @PostMapping("/list")
    @Operation(summary = "用户列表")
    public ResponseResult listUser(@RequestBody PageRequest<SimpleSo> so) {
        return R.success(sysUserService.listUser(new Page<>(so.getCurrent(), so.getSize()), so.getQuery()));
    }

    @GetMapping("/info")
    @Operation(summary = "用户信息")
    public ResponseResult getUserInfo(){
        return R.success(sysUserService.getUserInfo());
    }

    @GetMapping("/roleList")
    @Operation(summary = "用户角色列表")
    public ResponseResult getUserRole(@RequestParam("userId")String userId){
        return R.success(sysUserService.listRoleByUserId(Long.valueOf(userId)));
    }

    @PostMapping("/add")
    @Operation(summary = "新增用户")
    public ResponseResult addUser(@RequestBody UserAddDto dto) {
        return R.success(sysUserService.addUser(dto));
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户")
    public ResponseResult deleteUser(@RequestParam("id") String id) {
        return R.success(sysUserService.deleteUser(Long.valueOf(id)));
    }

    @PostMapping("/saveRole")
    @Operation(summary = "保存用户角色")
    public ResponseResult saveRole(@RequestBody SaveUserRoleDto dto) {
        return R.success(sysUserService.saveRole(dto));
    }
}
